﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;

namespace EHTWebsiteProject
{
    public partial class IndexLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtEmail.Focus();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                MembershipUser theUser = Membership.GetUser(this.txtEmail.Text.Trim());

                if (theUser == null)
                {
                    this.ltrMessage.Text = "Konto er ikke utgangene!";//Account is not exits
                }
                else
                {
                    //tài khoản bị khóa
                    //if (theUser.IsLockedOut == true)
                    //{
                    //    this.ltrMessage.Text = "Kontoen er stengt";//The account is closed
                    //    return;
                    //}

                    //ProfileBase theProfile = ProfileBase.Create(this.txtEmail.Text.Trim());
                    //string StatusActive = theProfile.GetPropertyValue("StatusActive").ToString();
                    //if (StatusActive == "0")
                    //{
                    //    this.ltrMessage.Text = "Konto er ikke aktiv";//Account is not active
                    //    return;
                    //}
                    //tài khoản chưa được duyệt bởi Admin
                    //if (theUser.IsApproved == false)
                    //{
                    //    this.ltrMessage.Text = "Your Account is not Approved";
                    //    return;
                    //}

                    if (Membership.ValidateUser(this.txtEmail.Text, this.txtPassword.Text))
                    {
                        String roleName = Roles.GetRolesForUser(theUser.UserName)[0];
                        if (Request.QueryString["ReturnUrl"] != null)
                        {
                            if (roleName == HangSo.ROLE_USER)
                            {
                                FormsAuthentication.RedirectFromLoginPage(this.txtEmail.Text.Trim(), false);
                            }

                        }
                        else
                        {
                            if (ckRememberMe.Checked == true)
                            {
                                FormsAuthentication.SetAuthCookie(theUser.UserName, true);
                            }
                            else
                            {
                                FormsAuthentication.SetAuthCookie(theUser.UserName, false);
                            }

                            // tài khoản thuộc nhóm Customer về trang Customer
                            if (roleName == HangSo.ROLE_USER)
                            {
                                Session["Username"] = theUser.UserName;
                                Response.Redirect("Hjem.html");
                            }
                            // tài khoản thuộc nhóm Admin thì về trang admin   
                            else if (roleName == HangSo.ROLE_ADMIN)
                            {
                                this.ltrMessage.Text = "Passord er ikke riktig";//Password is not correct
                            }
                        }
                    }
                    else
                    {
                        this.ltrMessage.Text = "Passord er ikke riktig";
                    }
                }
            }
        }
    }
}